<template>
  <div class="container-tags">
    <el-card>
      <!-- <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>学科管理</el-breadcrumb-item>
        <el-breadcrumb-item>前端</el-breadcrumb-item>
        <el-breadcrumb-item>目录管理</el-breadcrumb-item>
      </el-breadcrumb> -->

      <el-row>
        <el-col :span="18">
          <el-form :inline="true" label-width="80px" size="small">
            <el-form-item label="标签名称">
              <el-input v-model="tagsParams.tagName" style="200px"></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="tagsParams.tagStat">
                <el-option :value="1" label="启用"></el-option>
                <el-option :value="0" label="禁用"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item style="text-align: right">
              <el-button type="primary" @click="clear()">清除</el-button>
              <el-button type="success" @click="search()">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col style="text-align: right" :span="6">
          <el-button
            type="success"
            @click="modify()"
            size="small"
            icon="el-icon-edit"
            >新增标签</el-button
          >
        </el-col>
      </el-row>

      <el-alert
        :title="`数据一共${total}条`"
        style="margin-bottom: 15px"
        type="info"
        show-icon
        :closable="false"
      >
      </el-alert>
      <el-table :data="tags">
        <el-table-column type="index" label="序号" width="80px">
        </el-table-column>
        <el-table-column prop="subjectName" label="所属学科"> </el-table-column>
        <el-table-column prop="tagName" label="标签名称"> </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column prop="addDate" label="创建日期"> </el-table-column>
        <el-table-column prop="name" label="状态" width="180">
          <template slot-scope="scope">
            {{ scope.row.state === 1 ? "已启用" : "已禁用" }}
          </template>
        </el-table-column>
        <el-table-column prop="name" label="操作" width="180">
          <template slot-scope="scope">
            <el-link
              type="primary"
              v-if="scope.row.state === 0"
              @click="trigger(scope.row)"
              >启用</el-link
            >
            <el-link type="primary" v-else @click="trigger(scope.row)"
              >禁用</el-link
            >
            <el-link
              type="primary"
              style="margin: 0 10px"
              @click="modify(scope.row)"
              :disabled="scope.row.state === 1 ? true : false"
              >修改</el-link
            >
            <el-link
              type="primary"
              @click="delate(scope.row)"
              :disabled="scope.row.state === 1 ? true : false"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="tagsParams.page"
          :page-size="tagsParams.pages"
          layout="total, prev, pager, next"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>

    <!-- 弹出层 -->

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      @close="dialogVisible = false"
      width="30%"
    >
      <el-form :model="subjectsData" label-width="100px">
        <el-form-item label="所属学科">
          <el-select
            v-model="subjectsData.subjectID"
            placeholder="请选择活动区域"
          >
            <el-option
              v-for="item in subjectsList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="所属学科">
          <el-input v-model="subjectsData.tagName"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="modifyForm"> 确 定 </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { list, changeState, remove, update, add } from "../../api/hmmm/tags";

import { simple } from "../../api/hmmm/subjects";

export default {
  name: "skjds",
  data() {
    return {
      tagsParams: {
        tagName: null,
        tagStat: null,
        page: 1,

        pagesize: 10,
      },
      total: 0,
      tags: [],
      AAA: {},
      // 控制弹出层隐藏显示
      dialogVisible: false,
      // 学科下拉列表数据
      subjectsList: [],

      // 弹出层参数
      subjectsData: {
        subjectID: null,
        tagName: "",
        id: null,
      },
    };
  },
  created() {
    this.getList();
    this.getSubjectsList();
  },
  methods: {
    async getList(props) {
      const { data } = await list(props);
      this.total = data.counts;
      this.tags = data.items;
      this.pages = data.pages;
      console.log(data);
    },
    async getSubjectsList() {
      const { data } = await simple();
      this.subjectsList = data;
    },
    clear() {
      this.tagsParams = {
        tagName: null,
        tagStat: null,
        page: 1,
        pages: null,
        pagesize: 10,
      };
      this.getList();
    },
    search() {
      this.getList(this.tagsParams);
    },
    add() {},
    handleSizeChange(val) {
      this.tagsParams.pagesize = val;
      this.getList(this.tagsParams);
    },
    handleCurrentChange(val) {
      this.tagsParams.page = val;
      this.getList(this.tagsParams);
    },
    // 禁用状态
    async trigger(row) {
      this.AAA.id = row.id;
      this.AAA.state = row.state;
      if (row.state !== 1) {
        this.AAA.state = 1;
      } else {
        this.AAA.state = 0;
      }
      await changeState(this.AAA);
      this.getList();
    },
    modify(data) {
      if (data) {
        this.subjectsData.subjectID = data.subjectID;
        this.subjectsData.tagName = data.tagName;
        this.subjectsData.id = data.id;
      } else {
        this.subjectsData.id = null;
      }
      this.dialogVisible = true;
    },
    // add () {
    //   console.log(1)
    // },
    async delate(row) {
      await remove(row);
      this.$message.success("删除成功");
      this.getList();
    },

    // 弹出层确定
    async modifyForm() {
      if (this.subjectsData.id) {
        const { data } = await update(this.subjectsData);
        if (data.success) {
          this.$message.success("修改成功");
        } else {
          this.$message.error("修改失败");
        }
      } else {
        const { data } = await add(this.subjectsData);
        console.log(data);
        if (data.id) {
          this.$message.success("添加成功");
        } else {
          this.$message.error("添加失败");
        }
      }

      this.dialogVisible = false;
      this.getList();
    },
  },
};
</script>

<style scoped lang="scss">
.container-tags {
  padding: 10px;
}
</style>
